﻿using System;
using System.Collections;

namespace Hsenl.Network {
    [Serializable]
    public sealed class NetworkClient : Network {
        private Connector _connector;
        public override Service Service => this._connector;

        public bool IsConnected => this._connector?.IsConnected ?? false;

        public async HTask<int> Start(Connector connector) {
            this._connector = connector;
            this._connector.OnMessageReadedEvent += this.OnRecvMessage;
            var ret = await this._connector.ConnectAsync();
            return ret;
        }

        protected override void OnDestroy() {
            base.OnDestroy();
            this._connector.Dispose();
            this._connector = null;
        }

        public void Notif<T>(T message) where T : INotifMessage {
            base.Notif(message, 0);
        }

        public void Notif<T>(ref T message) where T : INotifMessage {
            base.Notif(ref message, 0);
        }

        public RpcInfo<TRequest, TResponse> Call<TRequest, TResponse>(TRequest message) where TRequest : IRpcMessage where TResponse : IRpcMessage {
            return base.Call<TRequest, TResponse>(message, 0);
        }

        public RpcInfo<TRequest, TResponse> Call<TRequest, TResponse>(ref TRequest message) where TRequest : IRpcMessage where TResponse : IRpcMessage {
            return base.Call<TRequest, TResponse>(ref message, 0);
        }
    }
}